package example;

/*
 * HOPE U LIKE IT SO FAR 
 * C LGPL 2012 matthias standfest aka m@w0rk
 */

import example_gui.*;
import gui5e.*;
import gui5e_modules.*;

import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
import processing.core.*;
import controlP5.*;
import peasy.*;


public class Init extends PApplet{
	private static final long serialVersionUID = 8119519305402207583L;
	// mouse and navigation stuff
	PeasyCam peasy;
	MouseWheelListenerClass wheel=new MouseWheelListenerClass();
	private boolean is3D;
	//control stuff
	public ControlP5 controlP5;
	// gui stuff
	public  PanelLeftA LP;
	public PanelRightA RP;
	public PanelBottomA BP;
	public PanelTopA TP;
	private static boolean isFullScreen = false;
	public Preferences preferences;
	public boolean isSetup = false;
	public int framerate = 60;
	public GUI gui = new GUI(this);
	public Viewport VP = new Viewport(this);
	
	
	public static void main(String _args[]) {
		if(isFullScreen)
			PApplet.main(new String[] {"--present", Init.class.getName() });
		else 
			PApplet.main(new String[] {Init.class.getName() });
	}
	
	public void setup(){
		  if(!isSetup){
		  if(isFullScreen) size(screenWidth, screenHeight, OPENGL);
		  else  size((screenWidth -200), (screenHeight-200), OPENGL);
		  isSetup=true;
		  }
		  //framerate
		  frameRate(framerate);
		  //peasy stuff
		  peasy = new PeasyCam(this, 200);
		  peasy.setMinimumDistance(50);
		  // http://processing.org/discourse/yabb2/YaBB.pl?num=1242718131
		  // alt+tab hack fpr controlP5 (elseway it stops refreshing!)
		  Keyboard k = new Keyboard();
		  KeyboardFocusManager.getCurrentKeyboardFocusManager() 
		    .addKeyEventDispatcher( k ); 
		 //controlP5 stuff
		  smooth();
		  controlP5 = new ControlP5(this);
		  controlP5.setAutoDraw(false);
		 // PANELS
		  LP = PanelLeftA.create(this, controlP5);  
		  RP = PanelRightA.create(this, controlP5);  
		  BP = PanelBottomA.create(this, controlP5);  
		  TP = PanelTopA.create(this, controlP5);  
		  TP.mode_='h';
		  BP.mode_='h';
		  LP.MP.setStyles();
		}
	
	  // http://processing.org/discourse/yabb2/YaBB.pl?num=1242718131
	  // alt+tab hack fpr controlP5 (elseway it stops refreshing!)
	class Keyboard implements KeyEventDispatcher
	{ 
	  Keyboard() 
	  { 
	} 
	  // http://processing.org/discourse/yabb2/YaBB.pl?num=1242718131
	  // alt+tab hack fpr controlP5 (elseway it stops refreshing!)
	  public boolean dispatchKeyEvent( KeyEvent ke )
	  { 
	    int kc = ke.getKeyCode();
	    if ( kc == 18 )  //18 = Alt key
	    {
	      return true;
	    }
	    else {
	      return false;  //retarget key event to application
	    }
	  } 
	} 
 // EO Alt+tab Hack
	public void draw() {
			 background(0);
			    peasy.beginHUD(); //2D CONTENT ON
			    //displayMain(); additional stuff
			    VP.draw();
			    peasy.endHUD();

			    peasy.beginHUD(); //2D CONTENT ON TOP    
			    LP.draw(this);
			    RP.draw(this);
			    BP.draw(this);
			    TP.draw(this);
			    peasy.endHUD();
			   // peasy.reset();
			    if(gui.deleteMe!=null){
				    for(ControllerInterface i : gui.deleteMe){
				    	i.remove();
				    }
				    gui.deleteMe.clear();
			    }
			  }


		public void keyPressed() {
			  switch(key) {
				  case 'x': System.exit(0); break;
				  case 'a':{
					  is3D=!is3D;
					  PApplet.main(new String[] {"--present", Init.class.getName() });
					  break;
				  	}
				  case '1': if(LP.mode_=='v') LP.mode_='h'; else LP.mode_='s';break;
				  case '2': if(BP.mode_=='v') BP.mode_='h'; else BP.mode_='s';break;
				  case '3': if(RP.mode_=='v') RP.mode_='h'; else RP.mode_='s';break;
				  case '4': if(TP.mode_=='v') TP.mode_='h'; else TP.mode_='s';break;
			  }
			}

			public void displayMain() {
			}


			public void mouseDragged() {
			  if (mouseEvent.getModifiers()==20 || mouseEvent.getModifiers()==8) {
			    GUI.trrX+=(mouseX-pmouseX)/GUI.sc;  
			    GUI.trrY+=(mouseY-pmouseY)/GUI.sc;
			  }
			}

			public class MouseWheelListenerClass implements MouseWheelListener {
				  public MouseWheelListenerClass() {
					  addMouseWheelListener(this);
				  }
				
				public void mouseWheelMoved(MouseWheelEvent e) {
				    if (!is3D) {
				      if (e.getWheelRotation()>0) {
				        GUI.sc *= 1.03;
				      } 
				      else if (e.getWheelRotation()<0) {
				        GUI.sc *= 1.0/1.03;
				      }
				    }
				  } 
			}			
			
		 public void controlEvent(ControlEvent theEvent) {
			 if(GUI.eventListener != null){
				 for(Module m: GUI.eventListener){
					 m.setEvent(theEvent); // give event to registered modules
				 }
			 }
		  }
		  
}